#!/usr/bin/env python 
# -*- coding:utf-8 -*-
# author:Dr.Shang


import tensorflow as tf
import cv2


filename = 'TFRecords/train.tfrecords'
filename_queue = tf.train.string_input_producer([filename])

reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) # 返回文件名和文件
features = tf.parse_single_example(serialized_example,
                                   features={
                                       'label': tf.FixedLenFeature([], tf.int64),
                                       'image': tf.FixedLenFeature([], tf.string)
                                   })

img = tf.decode_raw(features['image'], tf.uint8)
img = tf.reshape(img, [500, 500, 3])

img = tf.cast(img, tf.float32) * (1. / 500)
label = tf.cast(features['label'], tf.int32)

img_batch, label_batch = tf.train.shuffle_batch([img, label], batch_size=1, capacity=10, min_after_dequeue=6)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

for _ in range(2):
    img = sess.run(img_batch)
    label = sess.run(label_batch)
    img.resize((500, 500, 3)) # 没有这行报错
    cv2.imshow('test', img)
    cv2.waitKey()
    print(label)
